//
//  GiftViewController.m
//  TextApplication
//
//  Created by dllo on 16/8/29.
//  Copyright © 2016年 yh. All rights reserved.
//

#import "GiftViewController.h"
#import "TAMacro.h"
#import "GiftCollectionViewCell.h"
#import "GiftCollectionReusableView.h"
#import "CellDetailViewController.h" // 共用vc


@interface GiftViewController () <UICollectionViewDataSource, UICollectionViewDelegate, SendURL>
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) NSArray *idArray;

@property (nonatomic, strong) NSArray *picArray;

@end

@implementation GiftViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.idArray = @[@"1", @"2", @"3", @"4", @"5", @"6"];
    self.picArray = @[@"33", @"33", @"33", @"33", @"33"];
    [self createCollectionView];
    
}
- (void)createCollectionView {
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 64 + 35, WIDTH, HEIGHT - 99 - 44)];
    self.scrollView.contentSize = CGSizeMake(0, HEIGHT - 99 - 43);
    self.scrollView.backgroundColor = [UIColor colorWithWhite:0.152 alpha:1.000];
    [self.view addSubview:self.scrollView];
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake(WIDTH / 3, WIDTH / 3);
    layout.minimumInteritemSpacing = 0;
    layout.minimumLineSpacing = 0;
    layout.headerReferenceSize = CGSizeMake(0, 270);
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 99 - 44) collectionViewLayout:layout];
    self.collectionView.backgroundColor = [UIColor colorWithWhite:0.152 alpha:1.000];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.scrollView addSubview:self.collectionView];
   
    [self.collectionView registerClass:[GiftCollectionViewCell class] forCellWithReuseIdentifier:@"reuse"];
    // 自定义头视图
    [self.collectionView registerClass:[GiftCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headReuse"];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 6;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    GiftCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    
    [cell.picImageView setImage:[UIImage imageNamed:@"33"]];
    cell.backgroundColor = RandomColor;
    return cell;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    GiftCollectionReusableView *view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headReuse" forIndexPath:indexPath];
    view.delegate = self;
    return view;
}

- (void)sendURL:(NSString *)url {
    CellDetailViewController *detailVC = [[CellDetailViewController alloc] init];
    detailVC.giftURL = url;
    [self.navigationController pushViewController:detailVC animated:YES];
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    CellDetailViewController *detailVC = [[CellDetailViewController alloc] init];
    detailVC.giftURL = self.idArray[indexPath.row];
    NSLog(@"%@", self.idArray[indexPath.row]);
    [self.navigationController pushViewController:detailVC animated:NO];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
